import axios from 'axios'
import url from '../../util/url'
const classify = {
	namespaced: true,
	state: {
		list: [],
		pleibie: []
	},
	mutations: {

	},
	actions: {
		getList({ state }) {
			axios({
				method: 'get',
				url: url + 'api.php/records/classify',
				params: { order: 'id,asc' }
			}).then(res => {
				let plei = []
				res.data.records.map(obj => {
					let arr = []
					if (obj.pid > 0) {
						obj.pidTitle = '暂无数据'
						return
					}
					plei.push(obj)
					for (var i = 0; i < res.data.records.length; i++) {
						if (res.data.records[i].pid == obj.id) {
							arr.push(res.data.records[i].title)
						}
					}
					if (arr.length == 0) {
						obj.pidTitle = '暂无数据'
						return
					}
					obj.pidTitle = arr.join()
				})
				state.pleibie = plei
				state.list = res.data.records
			}).catch()
		},
		del({ dispatch }, obj) {
			axios({
				method: 'delete',
				url: url + 'api.php/records/classify/' + obj.id
			}).then(() => {
				dispatch('getList')
			}).catch()
		},
		saveItem({ dispatch }, obj) {
			axios({
				method: 'post',
				url: url + 'api.php/records/classify',
				data: obj
			}).then(() => {
				dispatch('getList')
			}).catch()
		}
	}
}
export default classify